package com.jalarbee.web.jsf.validator;

import com.jalarbee.stock.product.business.Quantity;
import com.jalarbee.stock.product.util.QuantityUtil;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

/**
 *
 * @author devel
 */

public class QuantityValidator implements Validator {

    @Override
    public void validate(FacesContext context, UIComponent component, Object object) throws ValidatorException {
        try {
            QuantityUtil.parse(((Quantity) object).toString());
        } catch(Exception exception) {
            FacesMessage message = new FacesMessage();
            message.setDetail(exception.getMessage());
            message.setSummary(exception.getMessage());
            message.setSeverity(FacesMessage.SEVERITY_ERROR);
            context.addMessage(component.getId(), message);
            throw new ValidatorException(message);
        }
    }

}
